

<?php
/**
 * found at: http://php.net/manual/de/language.oop5.patterns.php
 * 
 * edgar at goodforall dot eu 13-Jul-2009 01:48
 * Example of a decorator, which adds array access to any object:
 */
class ArrayAccessDecorator implements ArrayAccess {
        protected $object = null;
        public function __construct($object){
                $this->object = $object;
        }
        public function __call($method, $args){
                return $this->productpart->$method($args);
        }
        public function __set($key, $val){
                $this->object->$key = $val;
        }
        public function __get($key){
                return $this->object->$key;
        }
        /* ArrayAccess */
        public function offsetSet($key, $value){
                $this->__set($key, $value);
        }
        public function offsetGet($key){
                return $this->__get($key);
        }
        public function offsetUnset($key){
                $this->__set($key, null);
        }
        public function offsetExists($offset){

                        $test = $this->object->__get($offset);
                        if($test !== null){
                                return true;
                        } else {
                                return false;
                        }
        }
}

$stdobject = (object)array('test'=>'test', 'test2' => 'test2');
$arr = new ArrayAccessDecorator($stdobject);
print $arr['test']; // test
$arr['test'] = 'hello';
print $arr['test']; // hello


/**
 * ////////////////////////////////////////////////////////////////////////////
 * 
 */




?>
